package djboom.materials.display.elements {
	import flash.text.AntiAliasType;

	import djboom.materials.state.Font;
	import djboom.materials.events.PageEvent;

	import flash.events.MouseEvent;

	import org.casalib.display.CasaSprite;
	import org.casalib.display.CasaTextField;

	import flash.events.Event;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 07.04.2010
	 * 
	 */

	public class NavigationItem extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		public static const BLUE:uint = 0x004064;       
		public static const GRAY:uint = 0x888888;       
		public static const GREEN:uint = 0x709402;

		private static const TITLE_TFORMAT:TextFormat = new TextFormat(Font.MYRIAD_PRO_COND, 26, BLUE, null, true);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _page:String;

		private var _titleField:CasaTextField;

		private var _diamondGray:Diamond;
		private var _diamondBlue:Diamond;
		private var _diamondGreen:Diamond; 

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function NavigationItem(page:String, title:String, enable:Boolean) {
			super();
			
			_page = page;
			
			_titleField = new CasaTextField();
			_titleField.embedFonts = true;
			_titleField.selectable = false;
			_titleField.defaultTextFormat = TITLE_TFORMAT;			
			_titleField.autoSize = TextFieldAutoSize.LEFT;
			_titleField.antiAliasType = AntiAliasType.ADVANCED;
			_titleField.text = title;			
			_titleField.mouseEnabled = false;
			_titleField.x = 28;
			addChild(_titleField);
			
			if (enable) {
				this.buttonMode = true;
				this.useHandCursor = true;
				
				_diamondBlue = new Diamond(Diamond.BLUE);
				_diamondBlue.y = 9;
				addChild(_diamondBlue);
            
				addEventListener(MouseEvent.CLICK, clickItem);				addEventListener(MouseEvent.MOUSE_OVER, overItem);				addEventListener(MouseEvent.MOUSE_OUT, outItem);
			} else {
				_titleField.textColor = GRAY;
				
				_diamondGray = new Diamond(Diamond.GRAY);
				_diamondGray.y = 9;
				addChild(_diamondGray);
			}
			
			
            
			addEventListener(Event.REMOVED_FROM_STAGE, clearPage);
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function overItem(event:MouseEvent):void {
			_titleField.textColor = GREEN;
			
			if (_diamondBlue != null) {
				_diamondBlue.visible = false;
			} 
			
			if (_diamondGreen != null) {
				_diamondGreen.visible = true;
			} else {
				_diamondGreen = new Diamond(Diamond.GREEN);
				_diamondGreen.y = 9;
				addChild(_diamondGreen);	
			}
		}

		private function outItem(event:MouseEvent):void {
			_titleField.textColor = BLUE;
			
			if (_diamondBlue != null) {
				_diamondBlue.visible = true;
			}
            
			if (_diamondGreen != null) {
				_diamondGreen.visible = false;
			}
		}

		private function clickItem(event:MouseEvent):void {
			var pageEvent:PageEvent = new PageEvent(PageEvent.CHANGE);
			pageEvent.pageName = _page;            
			dispatchEvent(pageEvent);
		}

		private function clearPage(event:Event):void {
			if (_diamondGray != null) {
				_diamondGray.destroy();
				_diamondGray = null;
			}
			
			if (_diamondBlue != null) {
				_diamondBlue.destroy();
				_diamondBlue = null;
			}
            
			if (_diamondGreen != null) {
				_diamondGreen.destroy();
				_diamondGreen = null;
			}
			
			_titleField.destroy();
			_titleField = null;
		}
        
		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------
		
		// ----------------------------------------------------------------------------
        // Публичные свойства.
        // ----------------------------------------------------------------------------
	}
}
